/*
 * Thread.h
 *
 *  Created on: 03.01.2011
 *      Author: Tavin
 */

#include <stdint.h>
#include <stdio.h>

#include "JVM_StackFrame.h"
#include "JVM_Class.h"
#include "JVM_Method.h"
#ifndef JVM_THREAD_H_
#define JVM_THREAD_H_


#define THREAD_STATUS_NEW				0
#define THREAD_STATUS_RUNING			1
#define THREAD_STATUS_BLOCKED			2
#define THREAD_STATUS_WAITING			3
#define THREAD_STATUS_STOPPED			4
#define THREAD_STATUS_TIMED_WAITING		5
#define TERMINATED						6


typedef struct {
	void *next;
	JVM_StackFrame_t * currentStackframe;
	uint8_t priority;
	uint8_t status;
	uint64_t sleepCounter;

// status wait etc

} JVM_Thread_t;


JVM_Thread_t * threadList;
JVM_Thread_t * activeThread;

#endif /* JVM_THREAD_H_ */

void JVM_Thread_init();
JVM_Thread_t * JVM_Thread_newThread(JVM_Class_t* myClass, JVM_Method_t *myMethod);
void JVM_Thread_add(JVM_Thread_t *myThread);

void  JVM_Thread_killThread(JVM_Thread_t * threadToKill);
uint16_t  JVM_Thread_remove(JVM_Thread_t *myThread);

void JVM_Thread_countDownSleep();
boolean JVM_Thread_switchNext();

uint16_t JVM_Thread_executeClassInitThread();
uint16_t JVM_Thread_executeActiveThread();
JVM_Thread_t * JVM_Thread_getActiveThread();



uint16_t JVM_Thread_sleep();





/****************************************************************************************/
/*																						*/
/*	Function : JVM_Thread_setPriority						Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.1.3			Status : Working   			*/
/*																						*/
/****************************************************************************************/

void static inline JVM_Thread_setPriority(JVM_Thread_t* myThread, uint8_t priority)
{
	myThread->priority=priority;
}

/****************************************************************************************/
/*																						*/
/*	Function : JVM_Thread_setStatus							Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.1.3			Status : Working   			*/
/*																						*/
/****************************************************************************************/

void static inline JVM_Thread_setStatus(JVM_Thread_t* myThread, uint8_t status)
{
	myThread->status=status;
}





/****************************************************************************************/
/*																						*/
/*	Function : JVM_Thread_isRunning							Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.1.30			Status : Working   			*/
/*																						*/
/****************************************************************************************/

boolean static inline JVM_Thread_isRunning(JVM_Thread_t* myThread)
{
	if (myThread->status==THREAD_STATUS_RUNING)
		return(TRUE);

	return(FALSE);
}


/****************************************************************************************/
/*																						*/
/*	Function : JVM_Thread_setRunning						Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.1.30			Status : Working   			*/
/*																						*/
/****************************************************************************************/

void static inline JVM_Thread_setRunning(JVM_Thread_t* myThread)
{
	myThread->status=THREAD_STATUS_RUNING;
}


/****************************************************************************************/
/*																						*/
/*	Function : JVM_Thread_isStopped							Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.1.30			Status : Working   			*/
/*																						*/
/****************************************************************************************/

boolean static inline JVM_Thread_isStopped(JVM_Thread_t* myThread)
{
	if (myThread->status==THREAD_STATUS_STOPPED)
		return(TRUE);

	return(FALSE);
}


/****************************************************************************************/
/*																						*/
/*	Function : JVM_Thread_setStopped						Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.1.30			Status : Working   			*/
/*																						*/
/****************************************************************************************/

void static inline JVM_Thread_setStopped(JVM_Thread_t* myThread)
{
	myThread->status=THREAD_STATUS_STOPPED;
}

/****************************************************************************************/
/*																						*/
/*	Function : JVM_Thread_isWaiting							Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.1.30			Status : Working   			*/
/*																						*/
/****************************************************************************************/

boolean static inline JVM_Thread_isWaiting(JVM_Thread_t* myThread)
{
	if (myThread->status==THREAD_STATUS_WAITING)
		return(TRUE);

	return(FALSE);
}


/****************************************************************************************/
/*																						*/
/*	Function : JVM_Thread_setWaiting						Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.1.30			Status : Working   			*/
/*																						*/
/****************************************************************************************/

void static inline JVM_Thread_setWaiting(JVM_Thread_t* myThread)
{
	myThread->status=THREAD_STATUS_WAITING;
}

/****************************************************************************************/
/*																						*/
/*	Function : JVM_Thread_isSleeping						Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.1.30			Status : Working   			*/
/*																						*/
/****************************************************************************************/

boolean static inline JVM_Thread_isSleeping(JVM_Thread_t* myThread)
{
	if (myThread->status==THREAD_STATUS_TIMED_WAITING)
		return(TRUE);

return(FALSE);
}


/****************************************************************************************/
/*																						*/
/*	Function : JVM_Thread_setSleeping						Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.1.30			Status : Working   			*/
/*																						*/
/****************************************************************************************/

void static inline JVM_Thread_setSleeping(JVM_Thread_t* myThread)
{
	myThread->status=THREAD_STATUS_TIMED_WAITING;

}


/****************************************************************************************/
/*																						*/
/*	Function : JVM_Thread_getNext							Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.1.30			Status : Working   			*/
/*																						*/
/****************************************************************************************/

JVM_Thread_t  static inline *JVM_Thread_getNext(JVM_Thread_t* myThread)
{
	if (myThread->next==NULL)
		return(threadList);

return(myThread->next);
}


/****************************************************************************************/
/*																						*/
/*	Function : JVM_Thread_SetActive							Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.2.18			Status : Working   			*/
/*																						*/
/****************************************************************************************/

void static inline JVM_Thread_setActive(JVM_Thread_t* myThread)
{
	JVM_Thread_setRunning(myThread);
	activeThread=myThread;
}
